//
//  MMMovableText.m
//  MemeMaker
//
//  Created by Phan Quang Ha on 7/9/13.
//  Copyright (c) 2013 soyo. All rights reserved.
//

#import "MMMovableText.h"
#import "MMConstants.h"

@implementation MMMovableText

- (id)initWithText:(NSString *)text font:(UIFont *)font textAlignment:(NSTextAlignment)alignment andTextColor:(UIColor *)color
{
    CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(DEFAULT_TXT_WIDTH-2, 1000) lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat initWidth =  DEFAULT_TXT_WIDTH;
    CGFloat initHeight = size.height;
    
    
    if (self = [super initWithFrame:CGRectMake(0, 35, initWidth, initHeight)]) {
        self.mTextAlignment = alignment;
        self.mString = text;
        self.mFont = font;
        self.mTextColor = color;
    }
    return self;
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [super drawRect:rect];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, _mTextColor.CGColor);
    
    CGRect textFrame = CGRectMake(1, 1, self.frame.size.width - 2, self.frame.size.height - 2);
    [self.mString drawInRect:textFrame withFont:self.mFont lineBreakMode:NSLineBreakByWordWrapping alignment:self.mTextAlignment];
}

- (void)setMString:(NSString *)mString
{
    _mString = mString;
    CGSize size = [mString sizeWithFont:self.mFont constrainedToSize:CGSizeMake(DEFAULT_TXT_WIDTH-2, 1000) lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat initWidth =  DEFAULT_TXT_WIDTH;
    CGFloat initHeight = size.height;
    
    self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), initWidth, initHeight);
    
    [self setNeedsDisplay];
}
- (void)setMFont:(UIFont *)mFont
{
    _mFont = mFont;
    CGSize size = [self.mString sizeWithFont:mFont constrainedToSize:CGSizeMake(DEFAULT_TXT_WIDTH-2, 1000) lineBreakMode:NSLineBreakByWordWrapping];
    CGFloat initWidth =  DEFAULT_TXT_WIDTH;
    CGFloat initHeight = size.height;
    
    self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), initWidth, initHeight);
    [self setNeedsDisplay];
}
- (void)setMTextAlignment:(NSTextAlignment)mTextAlignment
{
    _mTextAlignment = mTextAlignment;
    [self setNeedsDisplay];
}
- (void)setMTextColor:(UIColor *)mTextColor
{
    _mTextColor = mTextColor;
    [self setNeedsDisplay];
}

#pragma mark - Gesture Edit
- (void)handlePinchAction:(UIPinchGestureRecognizer *)reg
{
//    [super handlePinchAction:reg];
//    
//    CGSize size = [self.mString sizeWithFont:self.mFont constrainedToSize:CGSizeMake(CGRectGetWidth(self.bounds), 1000) lineBreakMode:NSLineBreakByWordWrapping];
//    
//    self.frame = CGRectMake(CGRectGetMinX(self.frame), CGRectGetMinY(self.frame), size.width, size.height);
    
}

@end
